package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.Apply;
import com.rykj.qxj.server.model.entity.ApplyApi;
import com.rykj.qxj.server.model.entity.ApplyRelateParty;
import com.rykj.qxj.server.model.param.ApplyManagerParams;
import com.rykj.qxj.server.services.IApplyManageService;
import com.rykj.qxj.server.util.Pager;
import com.rykj.qxj.util.BeanValidator;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j
@RestController
@RequestMapping("/v1/applyManage")
@Api(tags="应用管理")
public class ApplyManageController {


    @Autowired
    private IApplyManageService applyManageService;
    @PostMapping("/saveApply")
    @ApiOperation(value="添加应用",notes ="添加应用")
    @EnableSystemLog(value="添加应用",fullPath ="/v1/applyManage/saveApply")
    public Result saveApply(@RequestBody  ApplyManagerParams applyManagerParams){
        try{
            BeanValidator.check(applyManagerParams.getApply());
            Apply apply = applyManagerParams.getApply();
            apply.setIssuance("Y");
            Apply checkApply =  applyManageService.checkApplyRep(apply.getApplyName(),apply.getApplyEng());
            if(null !=checkApply){
                if(apply.getApplyName().equals(checkApply.getApplyName())){
                    return new Result(false,"应用中文名称已存在");
                }else
                if(apply.getApplyEng().equals(checkApply.getApplyEng())){
                    return new Result(false,"应用英文简称已存在");
                }
            }
            applyManageService.saveApply(applyManagerParams);
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加应用信息失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/updateApply")
    @ApiOperation(value="修改应用信息",notes ="修改应用信息")
    @EnableSystemLog(value="修改应用信息",fullPath ="/v1/applyManage/updateApply")
    public Result updateApply(@RequestBody Apply apply){
        try{
            BeanValidator.check(apply);
            Apply checkApply =  applyManageService.checkApplyRep(apply.getApplyName(),apply.getApplyEng());
            if(null !=checkApply && apply.getId()!=checkApply.getId()){
                if(apply.getApplyName().equals(checkApply.getApplyName())){
                    return new Result(false,"应用中文名称已存在");
                }else
                if(apply.getApplyEng().equals(checkApply.getApplyEng())){
                    return new Result(false,"应用英文简称已存在");
                }
            }
            applyManageService.updateApply(apply);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改应用信息失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/updateApplyRelateParty")
    @ApiOperation(value="修改应用关联人信息",notes ="修改应用关联人信息")
    @EnableSystemLog(value="修改应用关联人信息",fullPath ="/v1/applyManage/updateApplyRelateParty")
    public Result updateApplyRelateParty(@RequestBody List<ApplyRelateParty> relateParties){
        try{
            applyManageService.updateApplyRelateParty(relateParties);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改应用关联人信息失败", e);
            return new Result(false, "修改应用关联人信息失败");
        }
    }

    @GetMapping("/deleteApplyById")
    @ApiOperation(value="删除应用信息",notes ="删除应用信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "应用id主键",required = true,paramType = "query")
    })
    @EnableSystemLog(value="删除应用信息",fullPath ="/v1/applyManage/deleteApplyById")
    public Result deleteApplyById( @RequestParam(value = "applyId") Integer applyId){
        try{
            applyManageService.deleteApplyById(applyId);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除应用信息失败", e);
            return new Result(false, "删除应用信息失败");
        }
    }

    @GetMapping("/getApplys")
    @ApiOperation(value="查询应用列表信息",notes ="查询应用列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "applyName", value = "应用名称",paramType = "query"),
            @ApiImplicitParam(name = "sysType", value = "系统类型:door门户 ",paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "类目id",paramType = "query"),
            @ApiImplicitParam(name = "issuance", value = "上线状态:Y上线,N未上线,空表示全部",paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "管理用户id",paramType = "query")
    })
    public Result getApplys( @RequestParam(value = "pageNum") Integer pageNum,
                             @RequestParam(value = "pageSize") Integer pageSize,
                             @RequestParam(value = "applyName",required=false) String applyName,
                             @RequestParam(value = "sysType",required=false) String sysType,
                             @RequestParam(value = "categoryId",required=false) Integer categoryId,
                             @RequestParam(value = "issuance",required=false) String issuance,
                             @RequestParam(value = "userId",required =false) Integer userId){
        try{
            Integer count =  applyManageService.getApplysCount(applyName,categoryId,issuance,userId);
            Integer pageCount = (count + pageSize - 1)/pageSize +1;
            if(pageNum>pageCount){
                log.error("页数不能超过总页数,总页数为:"+pageCount );
                return new Result(false,"页数不能超过总页数,总页数为:"+pageCount);
            }
            Pager pager =  applyManageService.getApplys(pageNum,pageSize,applyName,categoryId,issuance,userId,sysType);
            return new Result(pager);
        }catch (Exception e){
            log.error("查询应用列表信息失败", e);
            return new Result(false, "查询应用列表信息失败");
        }
    }

    @GetMapping("/getApplyDetails")
    @ApiOperation(value="应用详情",notes ="应用详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "应用id主键",required =true,paramType = "query"),
            @ApiImplicitParam(name = "sysType", value = "系统类型:door门户 ",paramType = "query")
    })
    public Result getApplyDetails( @RequestParam(value = "applyId") Integer applyId,
                                   @RequestParam(value = "sysType",required=false) String sysType){
        try{
            Apply apply   =  applyManageService.getApplyDetails(applyId);
            if("door".equals(sysType)){
                apply.setApplyId(null);
                apply.setApplyKey(null);
            }
            return new Result(apply);
        }catch (Exception e){
            log.error("应用详情失败", e);
            return new Result(false, "应用详情失败");
        }
    }


    @GetMapping("/getApplyRelateParties")
    @ApiOperation(value="查询应用关联负责人",notes ="查询应用关联负责人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "应用id",paramType = "query")
    })
    public Result getApplyRelateParties( @RequestParam(value = "applyId",required=false) Integer applyId){
        try{
            List<ApplyRelateParty> relateParties =  applyManageService.getApplyRelateParties(applyId);
            return new Result(relateParties);
        }catch (Exception e){
            log.error("查询应用列表信息失败", e);
            return new Result(false, "查询应用列表信息失败");
        }
    }
/*
    @GetMapping("/getApplyAccounts")
    @ApiOperation(value="查询应用管理/开发账号",notes ="查询应用管理/开发账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "应用id",paramType = "query")
    })
    public Result getApplyAccounts( @RequestParam(value = "applyId",required=false) Integer applyId,
                                    @RequestHeader("Token") String token){
        try{
            JSONArray applyAccounts =  applyManageService.getApplyAccounts(applyId,token);
            return new Result(applyAccounts);
        }catch (Exception e){
            log.error("查询应用列表信息失败", e);
            return new Result(false, "查询应用列表信息失败");
        }
    }*/


    @GetMapping("/getApplyAPIs")
    @ApiOperation(value="分页获取应用关联的API列表",notes ="分页获取应用关联的API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "apiName", value = "api名称",paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用id",required = true,paramType = "query")
    })
    public Result getApplyAPIs( @RequestParam(value = "pageNum") Integer pageNum,
                                @RequestParam(value = "pageSize") Integer pageSize,
                                @RequestParam(value = "apiName",required=false) String apiName,
                                @RequestParam(value = "applyId",required=false) Integer applyId){
        try{
            PageInfo pageInfo =  applyManageService.getApplyAPIs(pageNum,pageSize,apiName,applyId);
            return new Result(pageInfo);
        }catch (Exception e){
            log.error("获取应用关联的API列表失败", e);
            return new Result(false, "获取应用关联的API列表失败");
        }
    }

    @GetMapping("/getApplyUnboundAPIs")
    @ApiOperation(value="分页获取应用未绑定的api列表",notes ="分页获取应用未绑定的api列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "apiName", value = "api名称",paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用id",required = true,paramType = "query")
    })
    public Result getApplyUnboundAPIs( @RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       @RequestParam(value = "apiName",required=false) String apiName,
                                       @RequestParam(value = "applyId",required=false) Integer applyId,
                                       @RequestParam(value = "apiType",required=false) String apiType){
        try{
            PageInfo pageInfo =  applyManageService.getApplyUnboundAPIs(pageNum,pageSize,apiName,applyId);
            return new Result(pageInfo);
        }catch (Exception e){
            log.error("获取应用未绑定的API列表失败", e);
            return new Result(false, "获取应用未绑定的API列表失败");
        }
    }
/*    @GetMapping("/getApplyUnboundAPIs")
    @ApiOperation(value="分页获取应用未绑定的api列表",notes ="分页获取应用未绑定的api列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
    	@ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
    	@ApiImplicitParam(name = "apiName", value = "api名称",paramType = "query"),
    	@ApiImplicitParam(name = "applyId", value = "应用id",required = true,paramType = "query"),
    	@ApiImplicitParam(name = "apiType", value = "接口类型 subject:主题接口,isoutside:第三方接口",required = true,paramType = "query")
    })
    public Result getApplyUnboundAPIs( @RequestParam(value = "pageNum") Integer pageNum,
    		@RequestParam(value = "pageSize") Integer pageSize,
    		@RequestParam(value = "apiName",required=false) String apiName,
    		@RequestParam(value = "applyId",required=false) Integer applyId,
    		@RequestParam(value = "apiType",required=false) String apiType){
    	try{
    		PageInfo pageInfo =  applyManageService.getApplyUnboundAPIs(pageNum,pageSize,apiName,applyId,apiType);
    		return new Result(pageInfo);
    	}catch (Exception e){
    		log.error("获取应用未绑定的API列表失败", e);
    		return new Result(false, "获取应用未绑定的API列表失败");
    	}
    }
*/
    @PostMapping("/saveApplyApi")
    @ApiOperation(value="应用绑定API接口",notes ="应用绑定API接口")
    public Result saveApplyApi(@RequestBody List<ApplyApi> applyApis){
        try{
            applyManageService.saveApplyApi(applyApis);
            return new Result("绑定的成功");
        }catch (Exception e){
            log.error("绑定API接口失败", e);
            return new Result(false, "绑定API接口失败");
        }
    }

    @GetMapping("/deleteApplyApiById")
    @ApiOperation(value="解除应用绑定API接口",notes ="解除应用绑定API接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "应用Api关联id",required = true,paramType = "query")
    })
    @EnableSystemLog(value="解除应用绑定API接口",fullPath ="/v1/applyManage/deleteApplyApiById")
    public Result deleteApplyApiById(@RequestParam(value = "id") Integer id){
        try{
            applyManageService.deleteApplyApiById(id);
            return new Result("解除绑定成功");
        }catch (Exception e){
            log.error("解除绑定失败", e);
            return new Result(false, "解除绑定失败");
        }
    }

    @GetMapping("/updateApplyIssuance")
    @ApiOperation(value="应用上线/下线操作",notes ="应用上线/下线操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "issuance", value = "是否发布上线,N:否，Y:是",required = true,paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用主键id",required = true,paramType = "query")
    })
    @EnableSystemLog(value="应用上线/下线操作",fullPath ="/v1/applyManage/updateApplyIssuance")
    public Result updateApplyIssuance(@RequestParam(value = "issuance") String issuance,
                                      @RequestParam(value = "applyId") Integer applyId){
        try{
            applyManageService.updateApplyIssuance(issuance,applyId);
            return new Result("操作成功");
        }catch (Exception e){
            log.error("应用上线/下线", e);
            return new Result(false, "操作失败");
        }
    }

}
